package com.mt.demo.manager;

import com.corundumstudio.socketio.SocketIOClient;
import com.mt.demo.domain.MtMember;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class MeetingRoomManager {
    // 会议房间ID -> 房间中的客户端集合
    private final Map<String, Set<SocketIOClient>> roomClients = new ConcurrentHashMap<>();
    
    // 会议房间ID -> 屏幕共享用户ID
    private final Map<String, String> screenSharingUsers = new ConcurrentHashMap<>();
    
    // 客户端ID -> 最后心跳时间
    private final Map<String, Long> clientLastHeartbeat = new ConcurrentHashMap<>();
    
    // 客户端ID -> 用户信息
    private final Map<String, MtMember> clientUsers = new ConcurrentHashMap<>();

    // 添加客户端到房间
    public void addClientToRoom(String roomId, SocketIOClient client) {
        roomClients.computeIfAbsent(roomId, k -> ConcurrentHashMap.newKeySet()).add(client);
        updateClientHeartbeat(client.getSessionId().toString());
    }

    // 从房间移除客户端
    public void removeClientFromRoom(String roomId, SocketIOClient client) {
        Set<SocketIOClient> clients = roomClients.get(roomId);
        if (clients != null) {
            clients.remove(client);
            if (clients.isEmpty()) {
                roomClients.remove(roomId);
                screenSharingUsers.remove(roomId);
            }
        }
        clientLastHeartbeat.remove(client.getSessionId().toString());
        clientUsers.remove(client.getSessionId().toString());
    }

    // 更新客户端心跳时间
    public void updateClientHeartbeat(String clientId) {
        clientLastHeartbeat.put(clientId, System.currentTimeMillis());
    }

    // 获取客户端最后心跳时间
    public Long getClientLastHeartbeat(String clientId) {
        return clientLastHeartbeat.get(clientId);
    }

    // 设置房间屏幕共享用户
    public boolean setScreenSharingUser(String roomId, String userId) {
        if (!screenSharingUsers.containsKey(roomId)) {
            screenSharingUsers.put(roomId, userId);
            return true;
        }
        return false;
    }

    // 停止房间屏幕共享
    public void stopScreenSharing(String roomId) {
        screenSharingUsers.remove(roomId);
    }

    // 获取房间屏幕共享用户
    public String getScreenSharingUser(String roomId) {
        return screenSharingUsers.get(roomId);
    }

    // 获取房间所有客户端
    public Set<SocketIOClient> getRoomClients(String roomId) {
        return roomClients.getOrDefault(roomId, ConcurrentHashMap.newKeySet());
    }

    // 保存用户信息
    public void saveUserInfo(String clientId, MtMember member) {
        clientUsers.put(clientId, member);
    }

    // 获取用户信息
    public MtMember getUserInfo(String clientId) {
        return clientUsers.get(clientId);
    }

    // 获取房间在线用户数
    public int getRoomOnlineCount(String roomId) {
        Set<SocketIOClient> clients = roomClients.get(roomId);
        return clients != null ? clients.size() : 0;
    }

    // 清理超时客户端
    public void cleanupTimeoutClients(long timeoutMillis) {
        long currentTime = System.currentTimeMillis();
        clientLastHeartbeat.entrySet().removeIf(entry -> {
            if (currentTime - entry.getValue() > timeoutMillis) {
                String clientId = entry.getKey();
                clientUsers.remove(clientId);
                // 从所有房间中移除该客户端
                roomClients.values().forEach(clients -> 
                    clients.removeIf(client -> client.getSessionId().toString().equals(clientId)));
                return true;
            }
            return false;
        });
    }
}
